'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 角色相关
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var dataApi = require('../libs/dataApi.js');
var arenaModel = require('../models/arenaModel.js');
var soldierService = require('../services/soldierService.js');

/**
 * 创建竞技场NPC
 */
exports.createNpc = function (callback) {
    var first_npc_uid = 1;
    arenaModel.findNpc(first_npc_uid, function (err, arena_model) {
        if (!!err) {
            return callback(err);
        }

        if (!!arena_model) {
            console.log('exists NPC');
            return callback(null);
        }

        console.log('no NPC');
        var npc_list = [];
        dataApi.arena.each(function (item) {
            item.soldier_list = _getSoldierTypeList(item.soldier_info);
            npc_list.push(item);
        });

        // async.eachSeries(npc_list, function (npc, cb) {
        //     arenaModel.createArenaNpcRole(npc, cb);
        // }, function (err) {
        //     return callback(err);
        // });
        var npc_cre_list = [];
        _.forEach(npc_list, function (npc) {
            npc_cre_list.push({
                role_id: npc.id,
                npc_id: npc.id,
                rank: npc.id,
                att_soldier_list: npc.soldier_list,
                def_soldier_list: npc.soldier_list,
                att_power: npc.fp,
                def_power: npc.fp
            });
        });
        arenaModel.createNpcList(npc_cre_list, callback);
    });
};

/**
 * 获取批次的兵种数据
 * @return {Array} [description]
 */
var _getSoldierTypeList = function (soldier_info_str) {
    var soldier_type_list = [];
    if (soldier_info_str.length <= 0) {
        return soldier_type_list;
    }

    var soldier_info_list = soldier_info_str.split(',');
    _.forEach(soldier_info_list, function (soldier_info_item) {
        var soldier_info = soldier_info_item.split(':');
        var soldier_type = parseInt(soldier_info[0]);

        soldier_type_list.push(soldier_type);
    });
    return soldier_type_list;
};

/**
 * 更新竞技场NPC数据
 */
exports.updateArenaNpc = function (callback) {
    var npc_list = [];
    dataApi.arena.each(function (npc) {
        var soldier_list = _getSoldierTypeList(npc.soldier_info);
        npc_list.push({
            npc_id: npc.id,
            npc_info: {
                att_soldier_list: soldier_list,
                def_soldier_list: soldier_list,
                att_power: npc.fp,
                def_power: npc.fp
            }
        });
    });

    console.log('----update arena npc, count=', npc_list.length);
    async.eachSeries(npc_list, function (npc, cb) {
        arenaModel.updateNpc(npc.npc_id, {
            $set: npc.npc_info
        }, function (err, effect) {
            if (!!err) {
                console.error(err + '  -- ' + npc.npc_id);
                return cb(err);
            }
            console.log('--- success:', npc.npc_id);
            cb();
        });
    }, function (err) {
        if (!!err) {
            console.error(err);
            return callback();
        }
        return callback();
    });
};

/**
 * 更新玩家竞技场战力
 */
exports.uupdateArenaPower = function (player_manager, opt_type) {
    var arena_model = player_manager.arena;
    var soldier_model = player_manager.soldier;

    // 计算战力
    if (opt_type === 1) {
        arena_model.att_power = soldierService.calcSoldierPower(soldier_model, arena_model.att_soldier_list);
    } else {
        arena_model.def_power = soldierService.calcSoldierPower(soldier_model, arena_model.def_soldier_list);
    }
};
